<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->helper('captcha');
		$this->load->model('adminuser_m');
		$this->load->model('adminuserLoginLog_m');
		$this->load->library('Yzm');
	}
	/**
	 * 后台登陆页面
	 * @return [type] [description]
	 */
	public function index(){
		if(isset($_SESSION['eduadmininfo'])){
			$url=$this->_URL."Index/index";
			echo Js::top_to_url($url);exit;
		}
		//是否需要验证码
		$this->assign('admin_yzm',getConfig('admin_yzm'));

		$this->display('Login/index.html');
	}
	//修改密码
	public function pass(){
		if(isset($_POST['ajaxs']) &&  isset($_SESSION['eduadmininfo']) ){
			if(!cms_length($_POST['repass'],6)  || !check_regex($_POST['repass'],'isnozh') ){
				ajax_return(0,'密码长度不能小于6个英文与数字组成的字符');
			}
			$eduadmininfo=unserialize($_SESSION['eduadmininfo']);
			$where['id']=array('=',$eduadmininfo['id']);
			$result=$this->adminuser_m->sel($where);
			if(!$result){ajax_return(0,'不存在用户');}
			$pass=passcreate($_POST['pass']);
			if($pass!=$result['password']){
				ajax_return(0,'原密码错误');
			}
			$password=passcreate($_POST['repass']);
			$editdata['password']=$password;
			if($this->adminuser_m->edit($where,$editdata)===false){
				ajax_return(0,'密码修改失败');
			}
			ajax_return(1,'密码修改成功');
		}else{
			return false;
		}
	}
	/**
	 *登陆ajax验证
	 * @return [type] [description]
	 */
	public function login_ajax(){
		$data=form_ajaxs($_POST['formjquery']);
		//判断验证码是否开启
		$yzm_status=getConfig('admin_yzm');
		if($yzm_status==1){
			$yzmcode=$data['yzm'];
			if(!Yzm::returnCode($yzmcode)){
				ajax_return(0,'验证码错误');
			}
		}
		//判断是否锁定
		$where['loginname']=array('=',$data['loginname']);
		if(!cms_length($data['loginname'],5,16) || !check_regex($data['loginname'],'isnozh')){
			ajax_return(0,'账号长度为5-15个英文与数字组成的字符');
		}
		if(!cms_length($data['password'],6)  || !check_regex($data['password'],'isnozh') ){
			ajax_return(0,'密码长度不能小于6个英文与数字组成的字符');
		}
		$result=$this->adminuser_m->sel($where);
		if($result){
			//写入登陆日志公共信息
			$writedata['username']=$result['loginname'];
			$writedata['url']=$this->_URL."Login/index";
			if($result['locked']==1){
				ajax_return(0,'登陆多次失败已被锁定,请联系管理员');
			 }elseif($result['status']!=1){
				ajax_return(0,'账号已被禁用，请联系管理员');
			}elseif($result['password']!=passcreate($data['password'])){
				$writedata['description']='密码出错';
				if(!$this->adminuserLoginLog_m->addloginlog($writedata,false)){
					ajax_return(0,'写入登陆信息失败');
				}
				if(!$this->adminuserLoginLog_m->is_lock()){
					$this->adminuser_m->login_lock_type($result['id'],1);
					ajax_return(0,'登陆多次失败已被锁定,请联系管理员');
				}
				ajax_return(0,"密码出错");
			}else{
				//token 生成
				$usertoken=$editdata['usertoken']=guidstr($result['id']);
				$editdata['id']=$result['id'];
				//更新登陆成功后的信息
				if(!$this->adminuser_m->login_edit($editdata)){
					ajax_return(0,"更新用户状态出错");
				}
				//写入登陆日志
				if(!$this->adminuserLoginLog_m->addloginlog($writedata,true)){
					ajax_return(0,'写入登陆信息失败');
				}
				$result['usertoken']=$usertoken;
				unset($result['password']);
				$_SESSION['eduadmininfo']=serialize($result);
				ajax_return(1,'登陆成功');
			}
		}else{
			ajax_return(false,"不存在".$data['loginname']."管理员");
		}
	}
	public function verify(){
		$setconfig=[
			'width'=>120,//宽带
			'height'=>42,//高度
			'strlength'=>5,//字符长度
			'bgcolor'=>'#B0E0E6',//背景颜色 随机
			'pixelcolor'=>'#FFFFFF',//像素点颜色
			'textcolor'=>false,//字体颜色
			'linecolor'=>'#FFFFFF',//线条颜色
		];
		Yzm::setconfig($setconfig);
		return Yzm::run();
	}
	//退出登陆
	public function loginout(){
		if(isset($_SESSION['eduadmininfo'])){
			//更新用户下线状态
			$manageinfo=unserialize($_SESSION['eduadmininfo']);
			if(!$this->adminuser_m->loginout_edit($manageinfo['id'])){
				echo Js::alert('登出失败');
				echo Js::top_to_url($this->_URL.'Login/login');exit;
			}
			unset($_SESSION['eduadmininfo']);
		}
		echo Js::top_to_url($this->_URL."Login/index");
	}
}
